package com.euond.permission.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.euond.common.core.service.impl.BaseServiceImpl;
import com.euond.permission.entities.Resource;
import com.euond.permission.entities.ResourcePermission;
import com.euond.permission.mapper.ResourceMapper;
import com.euond.permission.service.ResourcePermissionService;
import com.euond.permission.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by user on 2017/7/24.
 */
@Service
public class ResourceServiceImpl extends BaseServiceImpl<ResourceMapper,Resource> implements ResourceService{
    @Autowired
    ResourcePermissionService resourcePermissionService;

    @Override
    @Transactional
    public boolean insert(Resource entity) {
        super.insert(entity);
        //同时写入资源权限值明细
        if(entity.getPermissionList()!=null) {
            //EntityWrapper entityWrapper=new EntityWrapper();
            //entityWrapper.eq("name",entity.getName());
            //Long resourceId=selectOne(entityWrapper).getId();
            //新增后mybatisplus会自动给entity赋ID
            Long resourceId=entity.getId();
            List<ResourcePermission> resourcePermissionList = entity.getPermissionList();
            for (int i = 0; i < resourcePermissionList.size(); i++) {
                resourcePermissionList.get(i).setResourceId(resourceId);
            }
            resourcePermissionService.insertBatch(resourcePermissionList);
        }
        return true;
    }

    @Override
    @Transactional
    public boolean updateById(Resource entity) {
        //修改：先删资源下面的权限值明细，再新增权限值明细，最后再修改资源本身
        resourcePermissionService.deleteResourcePermissionByResourceId(Long.valueOf(entity.getId()));
        if(entity.getPermissionList()!=null) {
            resourcePermissionService.insertBatch(entity.getPermissionList());
        }
        return super.updateById(entity);
    }

    @Override
    @Transactional
    public boolean deleteBatchIds(Collection<? extends Serializable> idList) {
        //先删资源权限值，后删资源
        for(int i=0;i<idList.toArray().length;i++){
            resourcePermissionService.deleteResourcePermissionByResourceId(Long.valueOf(idList.toArray()[i].toString()));
        }
        return super.deleteBatchIds(idList);
    }

    /**
     * 查询主表顺带返回从表数据
     *
     * @param searchParams
     */
    public Page<Resource> selectListPageByMap(Map<String, Object> searchParams) {
        Page<Resource> resourcePage =super.selectListPageByMap(searchParams);
        if (resourcePage.getTotal()>0){
            List<Resource> resourceList =resourcePage.getRecords();
            for(Resource resource:resourceList){
                resource.setPermissionList(resourcePermissionService.findResourcePermissionByResourceId(resource.getId()));
            }
            resourcePage.setRecords(resourceList);
        }
        return resourcePage;
    }
}
